package com.lancoo.znbkxx.teacher.aibkTestCoach.paperToEvaluation.aibkEvalutionZHT

import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.LancooMethod
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerViewHolder
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerAdapter
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.PaperEvaluationInfor
import com.lancoo.znbkxx.teacher.aibkTestCoach.paperToEvaluation.PaperEvaluationInforIndexHelp.getChildListBean
import com.lancoo.znbkxx.teacher.widget.CustomTextViewToPicture
import kotlinx.android.synthetic.main.aibk_adapter_item_child_xzt.view.*
import kotlinx.android.synthetic.main.aibk_adapter_item_childask.view.*
import kotlinx.android.synthetic.main.aibk_adapter_item_itemanalysis.view.*

/**
 * auther: 王国龙
 * Date：2020.05.22
 * describe:
 */
class EvaluationAdapterChildXzt(
        val activity: AppCompatActivity,
        val paperEvaluationInfor: PaperEvaluationInfor,
        val index_A: Int,
        val index_B: Int,
        val index_C: Int
) : KBaseRecyclerAdapter() {

    override fun getHolderView(parent: ViewGroup?, position: Int): View {
        var rootView: View? = null
        if (position == getChoiceTotalSize() + 1) {
            rootView = LayoutInflater.from(activity)
                    .inflate(R.layout.aibk_adapter_item_itemanalysis, parent, false)
        } else if (position == 0) {
            rootView = LayoutInflater.from(activity)
                    .inflate(R.layout.aibk_adapter_item_childask, parent, false)
        } else {
            rootView = LayoutInflater.from(activity)
                    .inflate(R.layout.aibk_adapter_item_child_xzt, parent, false)
        }
        return rootView
    }

    override fun onBindViewHolder(holder: KBaseRecyclerViewHolder, position: Int) {

        if (position == getChoiceTotalSize() + 1) {
            val childAnalysis = getChildListBean(paperEvaluationInfor, index_A, index_B, index_C).ChildAnalysis
            CustomTextViewToPicture(activity, holder.convertView.TvItemAnalysis, LancooMethod.getNewItemAnalysis(childAnalysis))
        } else if (position == 0) {
            val itemSortIndex = getChildListBean(paperEvaluationInfor, index_A, index_B, index_C).ItemList[0].ItemSortIndex
            val childAsk = getChildListBean(paperEvaluationInfor, index_A, index_B, index_C).ChildAsk
            holder.convertView.TvChildAskPositionX.text = "$itemSortIndex. "
            CustomTextViewToPicture(activity, holder.convertView.TvChildAsk, childAsk)

            //只有当小题中含有音频题干时才显示小题音频播放器，其余时候隐藏播放器
            val path = getChildListBean(paperEvaluationInfor, index_A, index_B, index_C).ChildAudio.Path
//            val path="https://lgresv57-mobile.obs.cn-south-1.myhuaweicloud.com/lgRs/82aa9b862d9045e7a06e37a687b57c43/8d9260f980d9b04b1c056fcb1b0cd262.mp3"
            if (path.isEmpty()) {
                holder.convertView.IvChildAudioPlayA.visibility = View.GONE
            } else {
                holder.convertView.IvChildAudioPlayA.visibility = View.VISIBLE
                childAudioListener!!.onInitChildAudioPlayer(path, holder.convertView.IvChildAudioPlayA)
            }

        } else {
            val optionIndex = getChildListBean(paperEvaluationInfor, index_A, index_B, index_C).ChildOptionList[position - 1].OptionIndex
            val option = getChildListBean(paperEvaluationInfor, index_A, index_B, index_C).ChildOptionList[position - 1].Option
            val showType = getChildListBean(paperEvaluationInfor, index_A, index_B, index_C).ChildOptionList[position - 1].ShowType
            val stuCount = getChildListBean(paperEvaluationInfor, index_A, index_B, index_C).ChildOptionList[position - 1].StuCount
            val rate = getChildListBean(paperEvaluationInfor, index_A, index_B, index_C).ChildOptionList[position - 1].Rate
            val answerDataDetail: String = activity.getResources().getString(R.string.aibk_evaluation_percent, stuCount, rate)
            if (showType == 0) {
                holder.convertView.BtnOptionIndex.text = optionIndex
                CustomTextViewToPicture(activity, holder.convertView.TvOptionContent, option + answerDataDetail)
            } else {
                holder.convertView.BtnOptionIndex.text = option
                CustomTextViewToPicture(activity, holder.convertView.TvOptionContent, answerDataDetail)
            }

            var itemAnswer = getChildListBean(paperEvaluationInfor, index_A, index_B, index_C).ItemList[0].ItemAnswer
            if (optionIndex == itemAnswer) {
                holder.convertView.BtnDuiGou.visibility = View.GONE
                holder.convertView.BtnOptionIndex.setTextColor(activity.resources.getColor(R.color.lancoooWhite))
                holder.convertView.BtnOptionIndex.setBackgroundResource(R.mipmap.aibk_icon_choice_green)
            } else {
                holder.convertView.BtnDuiGou.visibility = View.GONE
                holder.convertView.BtnOptionIndex.setTextColor(activity.resources.getColor(R.color.textColor))
                holder.convertView.BtnOptionIndex.setBackgroundResource(R.mipmap.aibk_icon_choice_white)
            }
        }

    }

    private fun getChoiceTotalSize(): Int {
        return getChildListBean(paperEvaluationInfor, index_A, index_B, index_C).ChildOptionList.size
    }

    override fun getItemCount(): Int {
        return getChoiceTotalSize() + 2
    }

    override fun getItemViewType(position: Int): Int {
        return position
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }


    interface OnChildAudioListener {
        fun onInitChildAudioPlayer(path: String, ivPlay: ImageView)
    }

    private var childAudioListener: OnChildAudioListener? = null

    fun setChildAudioListener(childAudioListener: OnChildAudioListener?) {
        this.childAudioListener = childAudioListener
    }

}